package com.mxw.account.thirdApi;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.http.MediaType;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Description TODO
 * @Author RingKo
 * @Date 2022/3/15 16:06
 * @Version 1.0
 */
@Slf4j
public class HamsaPayApi {
  public static void main(String[] args) {
//    String url = "https://qa-webapi.hamsapay.com/g1/api/v1/wallet/factoring/invoice/repayment";
    String url = "https://qa-webapi.hamsapay.com/g4/cerc/investor/signup";
    String json =
        "{\n"
            + "  \"email\": \"xinwen.ma+05070946@hamsapay.com\",\n"
            + "  \"password\": \"Mx!123456\",\n"
            + "  \"firstName\": \"aaa\",\n"
            + "  \"lastName\": \"bbb\",\n"
            + "  \"corporateName\": \"demoData\",\n"
            + "  \"incorporationCountry\": \"CN\",\n"
            + "  \"contactPersonName\": \"demoData\",\n"
            + "  \"phone\": \"18911111111\",\n"
            + "  \"corporateRegistrationNumber\": \"demoData\",\n"
            + "  \"agencyNumber\": \"abcdef\",\n"
            + "  \"accountNumber\": \"aaaaabbbbbcccccddddd\",\n"
            + "  \"acceptTermsOfService\": 1\n"
            + "}";
    String token = "Bearer " + "f7cf05a6-3024-4302-a5d8-c64a9d010898";
//    post(url, json, token, null);
    log.info(requestByUnirest(url, "", token, null));
  }

    public static String requestByUnirest(String url, String json,String token,File dataFile) {
      try {
      HttpResponse<String> response =
          Unirest.post(url)
              .header("authorization", token)
              .field(
                  "files",
                  new File(
                      "C:\\Users\\RingKo\\Desktop\\工作\\hamsapay\\工作记录\\statu machine\\batch factoring.png"),
                  MediaType.MULTIPART_FORM_DATA_VALUE)
              .field("data", json, MediaType.APPLICATION_JSON_VALUE)
              .asString();
        return response.getBody();
      } catch (UnirestException e) {
        log.error("HTTP Get Error:{}",e);
      }
      return "";
    }

  private static void post(String url, String json, String token, File dataFile) {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("authorization", token);
    CloseableHttpResponse response = null;

    try {
      MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
      //文件
      if (dataFile != null) {
        multipartEntityBuilder.addBinaryBody(
            "proofFiles", dataFile, ContentType.MULTIPART_FORM_DATA, dataFile.getName());
      }
      //数据
      ContentType contentType = ContentType.create("application/json", StandardCharsets.UTF_8);
      multipartEntityBuilder.addTextBody("data", json, contentType);

      HttpEntity httpEntity = multipartEntityBuilder.build();
      httpPost.setEntity(httpEntity);

      response = httpClient.execute(httpPost);
      HttpEntity responseEntity = response.getEntity();
      System.out.println("HTTPS响应状态为:" + response.getStatusLine());
      if (responseEntity != null) {
        System.out.println("HTTPS响应内容长度为:" + responseEntity.getContentLength());
        String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
        System.out.println("HTTPS响应内容为:" + responseStr);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (httpClient != null) {
          httpClient.close();
        }
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
